package com.kitty.game.look;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.RespTitle;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.message.RespCombatFriends_CL;
import com.kitty.game.fight.message.RespCombatOpponets_CL;
import com.kitty.game.fight.message.RespInitStatus;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.fight.service.BroadcastService;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.gate.model.QueueUpService;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.waiguan.CustomFasionIcon;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;


@Controller
@Slf4j
public class LookController {


    @Autowired
    QueueUpService queueUpService;

    @RequestMapping
    public void look(IoSession session, ReqLook reqLook) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null){
            return;
        }
        log.error("请求观战=={}", Json.toJson(reqLook));
        IoSession tosession = SessionUtils.getSession(reqLook.getRoleId());
        if (tosession == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("对方不存在"));
            return;
        }
        Role toRole = SessionUtils.getRoleBySession(tosession);
        if (toRole == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("对方不存在"));
            return;
        }
        FightService fightService = SpringUtils.getBean(FightService.class);
        if (fightService.isOnLook(role.getRoleId())){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("已经在观战中，不能再次进入观战"));
            return;
        }
        FightMessageService fightMessageService = SpringUtils.getFightMessageService();
        Fight fight = fightService.getFightByRoleId(toRole.getRoleId());
        if (fight == null){
            return;
        }
        FightObject roleObject = fight.queryFightObject(toRole.getRoleId());
        if (roleObject == null) {
            return;
        }
        MessagePusher.pushMessage(role,new RespMsg("正在准备观战中..."));
        RespTitle respTitle = new RespTitle();
        respTitle.setRoleId(role.getRoleId());
        respTitle.setList(new ArrayList<>());
        respTitle.getList().add((byte) 6);
        MessagePusher.pushMessage(role,respTitle);
        RespLook respLook = new RespLook();
        respLook.setState((short) 2);
        MessagePusher.pushMessage(role,respLook);
        List<CustomFasionIcon> customIconListA = fight.getCustomIconListA();
        List<CustomFasionIcon> customIconListB = fight.getCustomIconListB();
        /** 重连友方 */
        RespCombatFriends_CL respFightMemberA = new RespCombatFriends_CL();
        List<FightMember> fightMemberA = fightService.getFightMemberList(fight.getListA(), customIconListA);
        /** 重连敌方 */
        RespCombatOpponets_CL respFightMemberB = new RespCombatOpponets_CL();
        List<FightMember> fightMemberB = fightService.getFightMemberList(fight.getListB(), customIconListB);

        /** 发送站位包 */
        if (roleObject.isASide()) {
            respFightMemberA.setList(fightMemberA);
            respFightMemberB.setList(fightMemberB);
            respFightMemberA.setCustomIconList(customIconListA);
            respFightMemberB.setCustomIconList(customIconListB);
        } else {
            respFightMemberB.setList(fightMemberA);
            respFightMemberA.setList(fightMemberB);
            respFightMemberB.setCustomIconList(customIconListA);
            respFightMemberA.setCustomIconList(customIconListB);
        }
        MessagePusher.pushMessage(role, respFightMemberB);
        MessagePusher.pushMessage(role, respFightMemberA);
        /** 检测buff状态和死亡状态 */
        RespInitStatus respInitStatus = new RespInitStatus();
        respInitStatus.setList(new ArrayList<>());
        fight.getAllFightObjects().forEach(
                fightObject -> respInitStatus.getList().add(fightMessageService.checkCombatStatus(fightObject)));
        MessagePusher.pushMessage(role, respInitStatus);
        MessagePusher.pushMessage(role,new RespNotifyMiscEx("你进入了观战状态。"));
        fightService.addLook(role.getRoleId(),toRole.getRoleId());

    }


    /**
     * 退出观战
     * @param session
     * @param reqquitLook
     */
    @RequestMapping
    public void quitLook(IoSession session, ReqQuitLook reqquitLook) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null){
            return;
        }
        FightService fightService = SpringUtils.getBean(FightService.class);
        BroadcastService broadcastService = SpringUtils.getBroadcastService();
        Message message = broadcastService.getTitleInfo(role);
        MessagePusher.pushMessage(role,message);

        MessagePusher.pushMessage(role ,new RespLookEnd());
        MessagePusher.pushMessage(role,new RespNotifyMiscEx("你退出了观战。"));
        fightService.delLook(role.getRoleId());
    }
}
